package network.udp;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

//发送端，不需要连接服务器
public class UdpClient {

    public static void main(String[] args) {
        DatagramSocket socket = null;

        try {
            //1.建立一个数据包Socket
            socket = new DatagramSocket(4399);

            //2.服务器地址获取
            InetAddress localhost = InetAddress.getByName("localhost");
            int port = 8383;

            //3.建立包，定义数据格式,发送包
            String msg = "你好，我是Lby蓝。";
            DatagramPacket packet = new DatagramPacket(
                    msg.getBytes(StandardCharsets.UTF_8),0,msg.getBytes(StandardCharsets.UTF_8).length,localhost,port);
            socket.send(packet);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket!=null) {
                socket.close();
            }
        }

    }
}
